package org.ww.ucenter.controller;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.R;
import org.ww.commonutils.commentvo.MemberComment;
import org.ww.ucenter.bean.Member;
import org.ww.ucenter.service.MemberService;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-11-02
 */
@RestController
@RequestMapping("/ucenter/member")
//@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    //登录
    @PostMapping("/login")
    public R login(@RequestBody Member member){
        //使用jwt返回token
        String token=memberService.login(member);
        return R.ok().data("token",token);
    }


    //注册
    @PostMapping("/register")
    public R register(@RequestBody org.ww.ucenter.bean.vo.MemberVo memberVo){
        memberService.register(memberVo);
        return R.ok();
    }

    //根据Token获取用户信息
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        Member member=memberService.getUserInfo(request);
        return R.ok().data("member",member);
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserInfoById/{id}")
    public MemberComment getUserInfoById(@PathVariable String id){
        Member member=memberService.getById(id);
        MemberComment memberComment=new MemberComment();
        BeanUtils.copyProperties(member,memberComment);
        return memberComment;
    }

    /**
     * 得到指定日期注册的人数
     * @param date
     * @return
     */
    @GetMapping("/getRegisterMemberCount/{date}")
    public Integer getRegisterMemberCount(@PathVariable String date){
        Integer count=memberService.getRegisterMemberCount(date);
        return count;
    }
}

